﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UObject = UnityEngine.Object;
using UnityEngine;

namespace LuaFramework
{
    public class ResourceManager : Manager
    {
        /// <summary>
        /// 初始化
        /// </summary>
        public void Initialize(string manifestName)
        {
            //ShowResourceView(true);
            BundleManager.UnloadAllAssetBundles();
//#if UNITY_EDITOR
            if(!LoadManager.loadFromEditor)
            BundleManager.Init(manifestName);
//#endif
        }

        protected override void Update()
        {
            BundleManager.Update();
            AssetManager.Update();
        }

        string GetRealPath(string abName)
        {
            abName = abName.ToLower();
            if (abName == LoadManager.DATA)
            {
                return abName;
            }

            if (LoadManager.loadFromEditor)
            {
                if (abName.EndsWith(AppConst.ExtName))
                    abName = abName.Substring(0, abName.Length - AppConst.ExtName.Length);
            }
            else
            {
                if (!abName.EndsWith(AppConst.ExtName))
                    abName += AppConst.ExtName;
            }

            
            return abName;
        }

        /// <summary>
        /// 设置开启资源视图
        /// </summary>
        public void ShowResourceView(bool show)
        {
            var view = AppFacade.gameManager.GetComponent<ResourceView>();
            if (view == null)
                view = AppFacade.gameManager.gameObject.AddComponent<ResourceView>();
            view.enabled = show;
        }
        
        /// <summary>
        /// 是否开启资源视图
        /// </summary>
        public bool IsShowResourceView()
        {
            var view = AppFacade.gameManager.GetComponent<ResourceView>();
            return view != null && view.enabled;
        }

        #region Load Asset
        /// <summary>
        /// 同步加载Asset
        /// </summary>
        /// <typeparam name="T">Asset类型</typeparam>
        /// <param name="path">AssetBundle路径</param>
        /// <param name="name">指定AssetBundle中哪个Asset，为空时为第一个</param>
        /// <param name="source">资源来源</param>
        public UObject LoadAsset(string path, string name = null, System.Type assetType = null, LoadSource source = LoadSource.Auto)
        {
            return AssetManager.Load(GetRealPath(path), name, assetType, source);
        }

        public void LoadScene(string loadSceneName, bool isAdditive)
        {
#if !UNITY_EDITOR
            string bundlePath = "scenes/assetbundle/" + loadSceneName.ToLower() + ".unity";
            if (FileExists(bundlePath))
            {
                BundleManager.LoadTask(bundlePath + ".unity3d");
            }
            else
            {
                Debug.Log(bundlePath + " not Exists");
            }
#endif
            UnityEngine.SceneManagement.SceneManager.LoadScene(loadSceneName, isAdditive ? UnityEngine.SceneManagement.LoadSceneMode.Additive : UnityEngine.SceneManagement.LoadSceneMode.Single);
        }



        /// <summary>
        /// 异步加载Asset
        /// </summary>
        /// <param name="path">AssetBundle路径</param>
        /// <param name="name">指定AssetBundle中哪个Asset，为空时为第一个</param>
        /// <param name="assetType">资源类型</param>
        /// <param name="onFinish">加载完成回调</param>
        /// <param name="source">资源来源</param>
        /// <param name="priority">加载优先级</param>
        /// <returns>返回加载任务对象</returns>
        public LoadTaskR LoadAssetAsync(string path, string name = null, System.Type assetType = null, LoadSource source = LoadSource.Auto)
        {
            return AssetManager.LoadAsync(GetRealPath(path), name, assetType, source);
        }

        public IEnumerator LoadSceneAsync(string loadSceneName, bool isAdditive)
        {
#if !UNITY_EDITOR
            string bundlePath = "scenes/assetbundle/" + loadSceneName + ".unity";
            if (FileExists(bundlePath))
            {
                var loadBundleTask = BundleManager.LoadAsync(bundlePath + ".unity3d");
                yield return loadBundleTask;
            }
            else
            {
                Debug.Log(bundlePath + " not Exists");
            }
#endif
            yield return UnityEngine.SceneManagement.SceneManager.LoadSceneAsync(loadSceneName, isAdditive ? UnityEngine.SceneManagement.LoadSceneMode.Additive : UnityEngine.SceneManagement.LoadSceneMode.Single);
        }
        #endregion

        #region Load Data

        /// <summary>
        /// 判断文件是否存在
        /// </summary>
        public bool FileExists(string path)
        {
#if !UNITY_EDITOR
            path = path + ".unity3d";
#endif
            return LoadManager.FileExists(path);
        }

        /// <summary>
        /// 获取文件大小
        /// </summary>
        public long GetFileSize(string path)
        {
            return LoadManager.GetFileSize(path);
        }

        /// <summary>
        /// 同步加载文件，返回二进制数据
        /// </summary>
        public byte[] LoadData(string path)
        {
            return LoadManager.LoadData(path);
        }

        /// <summary>
        /// 读取文件文本
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="source">文件来源</param>
        /// <param name="encoding">文件编码</param>
        public string LoadText(string path, LoadSource source, Encoding encoding = null)
        {
            return LoadManager.LoadText(path, source, encoding);
        }

        /// <summary>
        /// 读取文件文本
        /// </summary>
        /// <param name="path">文件路径</param>
        public string LoadText(string path)
        {
            return LoadManager.LoadText(path);
        }
        #endregion

        /// <summary>
        /// 清理所有的bundle, 清理不需要的资源 
        /// </summary>
        public void Clear()
        {
            BundleManager.UnloadAllAssetBundles(true);

            GC.Collect();
        }

        public void ClearAssetBundles(string bundlePath)
        {
            string realPath = GetRealPath(bundlePath);
            BundleManager.UnloadAssetBundles(realPath);
            GC.Collect();
        }
    }
}